//
//  HomeActivityViewModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/10/28.
//

import UIKit
import MYModule
import RxSwift
import RxCocoa
import SwiftDate

open class HomeActivityViewModel: HomeBaseViewModel {
    
    /// UI数据
    public var UIReplay: BehaviorRelay<HomeActivityModel>
    
  
    public init(date: Date) {
        
        let step = HomeActivityModel.Record(target: 0, current: 0, unit: "")
        let calories = HomeActivityModel.Record(target: 0, current: 0, unit: "unit_calorie".language)
        let duration = HomeActivityModel.Record(target: 0, current: 0, unit: "unit_minute".language)
        
        let UIModel = HomeActivityModel(date: "--", step: step, calories: calories, duration: duration)
        
        UIReplay = BehaviorRelay<HomeActivityModel>(value: UIModel)
        
        super.init(cellType: .activity)
        reloadData(date)
        
     }
    
    
    public override func reloadData(_ date: Date? = nil) {
        
        guard let date = date else {
            return
        }

    
        var step_target: Double = 0
        var calories_target: Double = 0
        var duration_target: Double = 0
        
        var step_current: Double = 0
        var calories_current: Double = 0
        var duration_current: Double = 0
        
        var dateStr = ""
        if let stepModel = ActivityModel.day(date: date) {
            
            step_target = Double(stepModel.goalStep)
            calories_target = Double(stepModel.goalCalorie)
            duration_target = Double(stepModel.goalTime)

            step_current = Double(stepModel.step)
            calories_current = Double(stepModel.calorie)
            duration_current = Double(stepModel.time/60)
            
            dateStr = Self.dateStr(date: stepModel.date)
            
        } else {
            
            // 查询默认设置
            let model = ExerciseGoalsModel.currentGoal(weekDay: date.weekday)
            step_target = Double(model.step)
            calories_target = Double(model.kcal)
            duration_target = Double(model.exerciseTime)
            
        }
        
        let UIModel = UIReplay.value
        
        let step = UIModel.step
        let calories = UIModel.calories
        let duration = UIModel.duration
        
        step?.target = step_target
        step?.current = step_current

        calories?.target = calories_target
        calories?.current = calories_current
        duration?.target = duration_target
        duration?.current = duration_current
        
        UIModel.date = dateStr
    
        UIReplay.accept(UIModel)
        
    }
    
}
